﻿namespace Controllers
{
    using Domains;
    using System.IO;
    using System;

    /// <summary>
    /// Controller for load files for compare, comparing them and puts results to html file
    /// </summary>
    public static class LoadComparedFilesController
    {
        /// <summary>
        /// Load specified files, call compare and merge for these files
        /// </summary>
        /// <param name="previusVersion">Previus Version file</param>
        /// <param name="customVersion">Customer Version file</param>
        /// <param name="nextVersion">Next Version file</param>
        /// <returns>Array of 3 merged files (previus, customer, next)</returns>
        public static void Load(string previusVersion, string customVersion, string nextVersion)
        {
            //Compare Customer and Previus Version
            Execution.ExecCompareScript(customVersion, previusVersion, AppDomain.CurrentDomain.BaseDirectory + "CustomerPrevius.html");
            string[] CustomerPrevius = ComparedFiles.DivideComparationResult(AppDomain.CurrentDomain.BaseDirectory + "CustomerPrevius.html");
            //Compare Customer and Next Version
            Execution.ExecCompareScript(customVersion, nextVersion, AppDomain.CurrentDomain.BaseDirectory + "CustomerNext.html");
            string[] CustomerNext = ComparedFiles.DivideComparationResult(AppDomain.CurrentDomain.BaseDirectory + "CustomerNext.html");
            //Compare Previus Version and Next Version
            Execution.ExecCompareScript(previusVersion, nextVersion, AppDomain.CurrentDomain.BaseDirectory + "PreviusNext.html");
            string[] PreviusNext = ComparedFiles.DivideComparationResult(AppDomain.CurrentDomain.BaseDirectory + "PreviusNext.html");

            ComparedFiles.Merge(CustomerPrevius[0], CustomerNext[0], "Customer");
            StringToFile(ComparedFiles.Customer, Customer);
            Execution.ReadFile(Customer);

            ComparedFiles.Merge(CustomerPrevius[1], PreviusNext[0], "Previus");
            StringToFile(ComparedFiles.Previus, Previus);
            Execution.ReadFile(Previus);

            ComparedFiles.Merge(CustomerNext[1], PreviusNext[1], "Next");
            StringToFile(ComparedFiles.Next, Next);
            Execution.ReadFile(Next);

            NodeCount = ComparedFiles.NodesCount;
        }

        #region Private Methods

        /// <summary>
        /// Converting string to html file
        /// </summary>
        /// <param name="str">The string wich contain all Html data</param>
        /// <param name="filename">The name of the html file</param>
        private static void StringToFile(string str, string filename)
        {
            StreamWriter NewStreamWriter = new StreamWriter(filename);
            NewStreamWriter.WriteLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
            NewStreamWriter.WriteLine(str);
            NewStreamWriter.Close();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Customer file full name
        /// </summary>
        public static string Customer { get { return AppDomain.CurrentDomain.BaseDirectory + "Customer.html"; } }

        /// <summary>
        /// Previus file full name
        /// </summary>
        public static string Previus { get { return AppDomain.CurrentDomain.BaseDirectory + "Previus.html"; } }

        /// <summary>
        /// Next file full name
        /// </summary>
        public static string Next { get { return AppDomain.CurrentDomain.BaseDirectory + "Next.html"; } }

        /// <summary>
        /// Node count of loaded merged files
        /// </summary>
        public static int NodeCount { get; set; }

        #endregion
    }
}
